//
//  nextPageView.m
//  mintHome
//
//  Created by 朱彦名 on 16/6/21.
//  Copyright © 2016年 朱彦名. All rights reserved.
//

#import "nextPageView.h"
#import "tableViewOfAll.h"
#import "cellOfAll.h"
#import "NetWorkingHandler.h"
#import "modelOfTabView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "buttonOfTableViewController.h"

@interface nextPageView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) tableViewOfAll *tableViewOfAll1;

@property (nonatomic, retain) NSMutableArray *arrOfData;
@property (nonatomic, retain) NSMutableArray *arrOfRecieve;




@end

@implementation nextPageView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"全部专题";
    
    [self creatTableView];
    [self handleData];
    
    self.arrOfData = [NSMutableArray array];
    self.arrOfRecieve = [NSMutableArray array];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 布局
- (void)creatTableView{

    self.tableViewOfAll1 = [[tableViewOfAll alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableViewOfAll1.delegate = self;
    self.tableViewOfAll1.dataSource = self;
    
    [self.view addSubview:self.tableViewOfAll1];
    
    [self.tableViewOfAll1 registerClass:[cellOfAll class] forCellReuseIdentifier:@"pool"];
}
#pragma mark - 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    //NSLog(@"%ld",_arrOfRecieve.count);
    return _arrOfRecieve.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    cellOfAll *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"forIndexPath:indexPath];
    
    modelOfTabView *model1 = _arrOfRecieve[indexPath.row];
    
    BOOL model = [AccountManager sharedInstance].isNight;
    if (model == YES) {
        cell.backgroundColor = [UIColor silverColor];
    }else{
        cell.backgroundColor = [UIColor whiteColor];
        
    
    }
    [cell.image1 sd_setImageWithURL:[NSURL URLWithString:model1.cover_image_url]];
    cell.labelOfUp.text = model1.title;
    cell.labelOfDown.text = model1.subtitle;
    
    
    return cell;
    

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
    
}

#pragma mark - 解析数据
- (void)handleData{

    [MBProgressHUDManager showHUDtoView:_tableViewOfAll1 string:@"正在加载"];
    
    [NetWorkingHandler handleDataWithUrl:@"http://api.bohejiaju.com/v1/collections?limit=20&offset=0" completion:^(id result, NSURLResponse *response, NSError *error) {
        self.arrOfData = result [@"data"][@"collections"];
        for (NSDictionary *dic in _arrOfData) {
            
            modelOfTabView *model = [[modelOfTabView alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrOfRecieve addObject:model];
            
        }
        [_tableViewOfAll1 reloadData];
        
        [MBProgressHUDManager removeHUD];
        
        
    }];
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *url = @"http://api.bohejiaju.com/v1/collections/19/posts?gender=1&generation=1&limit=20&offset=0";
    buttonOfTableViewController *btmCol = [[buttonOfTableViewController alloc]init];
    
    for (int i = 19; i > 2; i--) {
        if (indexPath.row == 19 - i) {
            NSString *str = [NSString stringWithFormat:@"%d",i];
            
            NSString *newUrl = [url stringByReplacingOccurrencesOfString:@"19" withString:str];
            
            btmCol.urlRe = newUrl;
            
            
        }
    }
    
    
    
    [self.navigationController pushViewController:btmCol animated:YES];
    
    

}
- (void)setLightDay{
    
    
    [NightManager setBackgroundColorWithView:self.view];
    [_tableViewOfAll1 reloadData];
    
}
- (void)setNightDay{
    
    
    
    [NightManager setBackgroundColorWithView:self.view];
    [_tableViewOfAll1 reloadData];
    
}
@end
